#ifndef STATUSBARSTATE_H
#define STATUSBARSTATE_H

#include <QString>
#include "statusbarinfo.h"
#include "exportprogress.h"
#include "processingsettings.h"

/**
 * StatusBarState - 状态栏统一状态值对象
 *
 * Martin Fowler重构：Replace Data Clumps with Value Object
 * 将分散在多个类中的状态数据整合为单一值对象，建立单一数据源原则
 *
 * 遵循不可变性原则，所有状态变更通过工厂方法创建新实例
 */
class StatusBarState
{
public:
    // 默认构造函数 - 创建空闲状态
    StatusBarState() = default;

    // 完整构造函数
    StatusBarState(const StatusBarInfo& fileInfo,
                  int currentPage,
                  int totalPages,
                  const ProcessingSettings& settings,
                  qint64 cacheSize,
                  int cacheCount,
                  const QString& systemStatus,
                  const ExportProgress& exportProgress)
        : m_fileInfo(fileInfo)
        , m_currentPage(currentPage)
        , m_totalPages(totalPages)
        , m_settings(settings)
        , m_cacheSize(cacheSize)
        , m_cacheCount(cacheCount)
        , m_systemStatus(systemStatus)
        , m_exportProgress(exportProgress)
    {
    }

    // 工厂方法 - Martin Fowler重构：Replace Constructor with Factory Method
    static StatusBarState idle() {
        return StatusBarState(
            StatusBarInfo(),                    // 无文件
            0,                                  // 第0页
            0,                                  // 共0页
            ProcessingSettings(),               // 默认设置
            0,                                  // 无缓存
            0,                                  // 无缓存项
            "就绪",                             // 就绪状态
            ExportProgress::idle()              // 导出空闲
        );
    }

    static StatusBarState withFileInfo(const StatusBarState& state, const StatusBarInfo& fileInfo) {
        return StatusBarState(fileInfo, state.m_currentPage, state.m_totalPages,
                            state.m_settings, state.m_cacheSize, state.m_cacheCount,
                            state.m_systemStatus, state.m_exportProgress);
    }

    static StatusBarState withPageInfo(const StatusBarState& state, int currentPage, int totalPages) {
        return StatusBarState(state.m_fileInfo, currentPage, totalPages,
                            state.m_settings, state.m_cacheSize, state.m_cacheCount,
                            state.m_systemStatus, state.m_exportProgress);
    }

    static StatusBarState withSettings(const StatusBarState& state, const ProcessingSettings& settings) {
        return StatusBarState(state.m_fileInfo, state.m_currentPage, state.m_totalPages,
                            settings, state.m_cacheSize, state.m_cacheCount,
                            state.m_systemStatus, state.m_exportProgress);
    }

    static StatusBarState withCacheInfo(const StatusBarState& state, qint64 cacheSize, int cacheCount) {
        return StatusBarState(state.m_fileInfo, state.m_currentPage, state.m_totalPages,
                            state.m_settings, cacheSize, cacheCount,
                            state.m_systemStatus, state.m_exportProgress);
    }

    static StatusBarState withSystemStatus(const StatusBarState& state, const QString& systemStatus) {
        return StatusBarState(state.m_fileInfo, state.m_currentPage, state.m_totalPages,
                            state.m_settings, state.m_cacheSize, state.m_cacheCount,
                            systemStatus, state.m_exportProgress);
    }

    static StatusBarState withExportProgress(const StatusBarState& state, const ExportProgress& exportProgress) {
        return StatusBarState(state.m_fileInfo, state.m_currentPage, state.m_totalPages,
                            state.m_settings, state.m_cacheSize, state.m_cacheCount,
                            state.m_systemStatus, exportProgress);
    }

    // 访问器
    const StatusBarInfo& fileInfo() const { return m_fileInfo; }
    int currentPage() const { return m_currentPage; }
    int totalPages() const { return m_totalPages; }
    const ProcessingSettings& settings() const { return m_settings; }
    qint64 cacheSize() const { return m_cacheSize; }
    int cacheCount() const { return m_cacheCount; }
    const QString& systemStatus() const { return m_systemStatus; }
    const ExportProgress& exportProgress() const { return m_exportProgress; }

    // 业务逻辑查询
    bool hasFiles() const { return m_fileInfo.totalFiles() > 0; }
    bool hasPages() const { return m_totalPages > 0; }
    bool isExporting() const { return m_exportProgress.isInProgress(); }
    bool canExport() const { return hasFiles() && !isExporting(); }

    // 状态验证
    bool isValid() const {
        return m_currentPage >= 0 &&
               m_totalPages >= 0 &&
               m_currentPage <= m_totalPages &&
               m_cacheSize >= 0 &&
               m_cacheCount >= 0;
    }

    // 状态比较 - 用于优化更新
    bool fileInfoChanged(const StatusBarState& other) const {
        return m_fileInfo.currentIndex() != other.m_fileInfo.currentIndex() ||
               m_fileInfo.totalFiles() != other.m_fileInfo.totalFiles() ||
               m_fileInfo.fileName() != other.m_fileInfo.fileName();
    }

    bool pageInfoChanged(const StatusBarState& other) const {
        return m_currentPage != other.m_currentPage || m_totalPages != other.m_totalPages;
    }

    bool cacheInfoChanged(const StatusBarState& other) const {
        return m_cacheSize != other.m_cacheSize || m_cacheCount != other.m_cacheCount;
    }

    bool systemStatusChanged(const StatusBarState& other) const {
        return m_systemStatus != other.m_systemStatus;
    }

    bool exportProgressChanged(const StatusBarState& other) const {
        return m_exportProgress.completedFiles() != other.m_exportProgress.completedFiles() ||
               m_exportProgress.totalFiles() != other.m_exportProgress.totalFiles() ||
               m_exportProgress.status() != other.m_exportProgress.status();
    }

    // 调试输出
    QString toString() const {
        return QString("StatusBarState(files=%1, page=%2/%3, cache=%4MB, system=%5, export=%6)")
               .arg(m_fileInfo.formatText())
               .arg(m_currentPage).arg(m_totalPages)
               .arg(m_cacheSize / 1024 / 1024)
               .arg(m_systemStatus)
               .arg(m_exportProgress.status());
    }

private:
    // 状态数据
    StatusBarInfo m_fileInfo;           // 文件信息
    int m_currentPage = 0;              // 当前页面
    int m_totalPages = 0;               // 总页数
    ProcessingSettings m_settings;       // 处理设置
    qint64 m_cacheSize = 0;             // 缓存大小
    int m_cacheCount = 0;               // 缓存项数
    QString m_systemStatus = "就绪";     // 系统状态
    ExportProgress m_exportProgress;     // 导出进度
};

#endif // STATUSBARSTATE_H